Skip to content

Add aten::complex.out support for export and CPU execution - #24330

Open
mshr-h wants to merge 2 commits into
pytorch:mainfrom
mshr-h:add-aten-complex-out
Open

mshr-h wants to merge 2 commits into
pytorch:mainfrom
mshr-h:add-aten-complex-out

Conversation

@mshr-h

@mshr-h mshr-h commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Models using torch.complex(real, imag) currently fail the default to_edge() Core ATen check. Allowing the operator through that check still leaves CPU execution blocked by a missing aten::complex.out kernel.

This PR enables default export and CPU execution for matching FP16/32/64 inputs, producing complex32/64/128 outputs. The portable kernel supports broadcasting and output resizing, and requires default contiguous dimension order. Edge dtype mapping now includes complex32.

Note: aten.complex.default is temporarily allowed by the verifier while keeping IR validation enabled. A separate PyTorch change is needed to formally include it in Core ATen. This exception can be removed once all supported PyTorch versions include that change.

Fixes #24328

Test plan

Rebuild the native bindings and C++ tests from this branch, then

python -m unittest \
  executorch.extension.pybindings.test.test_pybindings.PybindingsTest.test_complex_construction \
  executorch.extension.pybindings.test.test_pybindings.PybindingsTest.test_complex_numpy_input \
  executorch.extension.pybindings.test.test_pybindings.PybindingsTest.test_complex_half_dlpack_input \
  executorch.exir.verification.test.test_verifier \
  executorch.exir.dialects.edge.test.test_edge_ops \
  executorch.exir.dialects.edge.test.test_edge_yaml

./cmake-out/kernels/test/portable_kernels_test \
  --gtest_filter='OpComplexOutTest.*'

ctest --test-dir cmake-out \
  -R '^portable_kernels_test$' --output-on-failure

Result: Python unittest reported 24 tests passed; GoogleTest reported 21/21 complex kernel tests passed; CTest reported 1/1 test target passed. No failures.

cc @larryliu0820 @manuelcandales @JakeStevens @nil-is-all

Copilot AI balanced review requested due to automatic review settings October 30, 2026 09:20
@pytorch-bot

pytorch-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24330

Note: Links to docs will display an error until the docs builds have been completed.

❌ You can merge normally! (4 Unrelated Failures), 4 Unclassified Failures

As of commit aba2e56 with merge base 7d51b01 (image):

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 30, 2026
@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from f04e979 to c2763d9 Compare October 30, 2026 09:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are blocking approval of the complex operator support.

No open issues 🎉

What changed in this PR

Enables torch.complex(real, imag) through default Edge export and CPU execution, addressing #24328 for matching FP16, FP32, and FP64 inputs.

Changes:

  • Adds a portable complex.out kernel with broadcasting, output resizing, and default dimension-order validation.
  • Registers the operator and adds complex32 dtype mapping plus a temporary Core ATen verifier exception.
  • Adds kernel and Python integration tests covering supported types, broadcasting, and invalid inputs.
File Description
shim_et/​xplat/​executorch/​kernels/​portable/​op_registration_util.bzl Adds the portable kernel build target and broadcast dependency.
kernels/​test/​targets.bzl Registers complex tests for ATen and portable kernels.
kernels/​test/​op_complex_test.cpp Tests types, broadcasting, special values, resizing, and validation.
kernels/​test/​CMakeLists.txt Includes complex kernel tests in CMake builds.
kernels/​portable/​functions.yaml Registers the portable complex.out implementation.
kernels/​portable/​cpu/​op_complex.cpp Implements complex tensor construction on CPU.
kernels/​aten/​functions.yaml Registers complex.out for ATen builds.
extension/​pybindings/​test/​test_pybindings.py Adds end-to-end export and execution coverage.
exir/​verification/​verifier.py Temporarily allows complex.default through Core ATen validation.
exir/​dialects/​edge/​dtype/​supported.py Maps complex32 to ComplexHalf.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 30, 2026 09:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues blocking the complex-construction support change.

No open issues 🎉

Review effort: Balanced

@mshr-h

mshr-h commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: ops & kernels"

@pytorch-bot pytorch-bot Bot added the release notes: ops & kernels Changes to the opset and any new / changed kernel implementations label Oct 30, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Oct 30, 2026
@nil-is-all nil-is-all added the module: kernels Issues related to kernel libraries and utilities, and code under kernels/ label Oct 30, 2026
@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from c2763d9 to 4b6aea4 Compare October 31, 2026 17:27
Copilot AI balanced review requested due to automatic review settings October 31, 2026 17:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the complex operator export and CPU runtime support changes.

No open issues 🎉

Review effort: Balanced

@nil-is-all

Copy link
Copy Markdown
Contributor

@mshr-h could you rebase branch with main and ping again for review?

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 4b6aea4 to 830615b Compare November 2, 2026 21:56
Copilot AI balanced review requested due to automatic review settings November 2, 2026 21:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the complex-construction export and CPU execution changes.

No open issues 🎉

Review effort: Balanced

@mshr-h

mshr-h commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@nil-is-all @JakeStevens Rebased onto the latest upstream/main. Could you take another look? Thanks!

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 830615b to 825068d Compare November 3, 2026 06:21
Copilot AI balanced review requested due to automatic review settings November 3, 2026 06:21
@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from b4345f8 to 441f2a6 Compare November 5, 2026 16:23
Copilot AI balanced review requested due to automatic review settings November 5, 2026 16:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the changes enabling torch.complex export and CPU execution.

No open issues 🎉

Review effort: Balanced

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 441f2a6 to 4451779 Compare November 6, 2026 16:29
Copilot AI balanced review requested due to automatic review settings November 6, 2026 16:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change enabling the default torch.complex export-to-CPU workflow.

No open issues 🎉

Review effort: Balanced

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 4451779 to 21f5700 Compare November 7, 2026 10:19
Copilot AI balanced review requested due to automatic review settings November 7, 2026 10:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the changes enabling torch.complex export and CPU execution.

No open issues 🎉

Review effort: Balanced

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 21f5700 to 61b4e38 Compare November 9, 2026 08:51
Copilot AI balanced review requested due to automatic review settings November 9, 2026 08:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change enabling torch.complex export and CPU execution.

No open issues 🎉

Review effort: Balanced

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 61b4e38 to 45559b7 Compare November 10, 2026 13:24
Copilot AI balanced review requested due to automatic review settings November 10, 2026 13:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the complex-construction changes.

No open issues 🎉

Review effort: Balanced

@JakeStevens

Copy link
Copy Markdown
Contributor

@claude review effort:max

@claude

claude Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Claude finished @JakeStevens's task in 3m 54s —— View job


The kernel is correct, but op_complex.cpp is missing from PORTABLE_KERNELS_SRCS, which should break selective CMake builds that pick aten::complex.out; the verifier exemption also has some design issues that are worth discussing.

Detailed review

PR Review: #24330

Summary

This PR adds a portable complex.out kernel. It also maps edge dtypes to complex32 and adds a temporary Core ATen verifier exemption, so torch.complex(real, imag) can export with default to_edge() and run on CPU.

Three reviewers looked at the PR independently, and all three found the same build-list gap. There is also a design question about how wide the verifier exemption reaches, plus some smaller test and coverage gaps.

Infrastructure

  • shim_et/xplat/executorch/build/build_variables.bzl:108: kernels/portable/cpu/op_complex.cpp is not in PORTABLE_KERNELS_SRCS (all 3 reviewers; I confirmed it).
    • Why CI won't catch it: kernels/portable/CMakeLists.txt globs cpu/*.cpp, so normal builds work.
    • What breaks: tools/cmake/Codegen.cmake:445-446 builds selected_portable_kernels from _portable_kernels__srcs, and that list comes from this file.
    • Trigger: a selective or dtype-selective CMake build that selects aten::complex.out. The generated registration refers to torch::executor::native::complex_out, but nothing compiles it, so the link should fail with an undefined symbol. I did not build this.
    • Fix: add "kernels/portable/cpu/op_complex.cpp", between op_clone.cpp and op_constant_pad_nd.cpp. Fix this →

API Design

  • exir/verification/verifier.py:147-149: the exemption is hard-coded and applies to every user.
    • Scope: _get_core_aten_ops_exception_list is used by both the ATen and Edge verifiers. Every to_edge() call now accepts aten.complex.default, whatever the target runtime. That includes delegate-only runtimes, where no backend claims the op.
    • Failure moves later: those users used to get a clear Core ATen error at export time. Now they get a missing-kernel error at runtime, or a link error in a selective build. The opt-in EdgeCompileConfig(_core_aten_ops_exception_list=[...]) path exists for this case, and its error message tells users to check that a kernel exists first.
    • Removal is untracked: the other entries carry TODO(T…) task IDs. This TODO has no issue, no link to the upstream PyTorch PR, and no version gate, so nothing will flag it as dead code once PyTorch tags complex as core.
    • Options:
      • Gate the exemption, for example only add it when torch.Tag.core not in torch.ops.aten.complex.default.tags.
      • At minimum, link the upstream PR or issue in the TODO.
      • Whether to keep it global or make it opt-in is a call for the maintainers.

Testing

  • No exir-level test.
    • The only end-to-end coverage is extension/pybindings/test/test_pybindings.py:1841, and it only runs when native bindings are built.
    • Nothing in exir/verification/test checks that to_edge() accepts torch.complex, or that it keeps working after the exemption is removed.
  • kernels/test/op_complex_test.cpp:218-238 (NonDefaultDimOrder) uses a pattern no other kernel test uses.
    • It loops over three cases inside one TEST_F and resets the fixture with context_ = {};. No other kernel test does this.
    • If one case fails, the output doesn't say which case it was.
    • Split it into three TEST_Fs, as the sibling tests do.
  • ComplexHalf mismatches will abort instead of printing a diff.
    • runtime/core/exec_aten/testing_util/tensor_util.cpp:334 only handles ET_FORALL_COMPLEX_TYPES, which doesn't include ComplexHalf.
    • So if OpComplexOutTest.Half ever fails, gtest's printer reaches ET_CHECK_MSG(false, "Unhandled dtype ...") and aborts the process instead of reporting the mismatch.
    • This is the first op test that compares ComplexHalf tensors. Switching that line to ET_FORALL_COMPLEXH_TYPES (already defined at scalar_type_util.h:363) fixes it.

Code Quality

  • exir/dialects/edge/dtype/supported.py:30: complex32 support only covers the case where the result is a graph output. Low confidence: I did not run it.
    • Adding torch.complex32 here makes ops with no edge.yaml entry, including complex.default, accept ComplexHalf through the fallback at exir/dialects/edge/_ops.py:230-240.
    • edge.yaml has no ComplexHalf entries at all.
    • So in an FP16 model, if the complex32 result feeds an op whose constraints come from edge.yaml, to_edge should still fail the edge dtype check. Examples are clone, view_copy and permute_copy.
    • Either narrow the PR description's FP16→complex32 claim, or regenerate the spec and add a test where the complex32 output is consumed by another op.
  • kernels/portable/cpu/op_complex.cpp:38-44: the dim-order check is stricter than in sibling binary kernels.
    • It requires tensor_is_default_dim_order on all three tensors. Ops like op_atan2.cpp use tensors_have_same_dim_order(a, b, out) instead.
    • So a channels-last export passes to_edge and then fails with InvalidArgument at runtime.
    • The PR description says this is intentional. Unless there's a reason the broadcast path can't handle matching non-default dim orders, tensors_have_same_dim_order would match the other kernels.

Recommendation

Request Changes

The only blocking item is the missing PORTABLE_KERNELS_SRCS entry, which is a one-line fix. The verifier exemption's scope and how it will be removed need a maintainer decision. The rest are minor.

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 45559b7 to 2a36833 Compare November 10, 2026 14:37
Copilot AI balanced review requested due to automatic review settings November 10, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the added torch.complex export and CPU execution support.

No open issues 🎉

Review effort: Balanced

@mshr-h
mshr-h force-pushed the add-aten-complex-out branch from 2a36833 to aba2e56 Compare November 10, 2026 14:41
@mshr-h

mshr-h commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed claude review by adding the missing PORTABLE_KERNELS_SRCS entry. Rebased on the latest main too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. community: contribution PRs coming from community (excluding hardware partners) module: kernels Issues related to kernel libraries and utilities, and code under kernels/ release notes: ops & kernels Changes to the opset and any new / changed kernel implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torch.complex fails Edge export and CPU execution

4 participants